package com.wenbo.wenbo.service.impl;


import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenbo.wenbo.entity.Birthday;
import com.wenbo.wenbo.mapper.BirthdayMapper;
import com.wenbo.wenbo.risk.ScheduledTask;
import com.wenbo.wenbo.service.BirthdayService;
import com.wenbo.wenbo.service.MsgService;
import com.wenbo.wenbo.service.WeChatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author ywb
 * @date 2021年12月08日 10:03
 */
@Service
public class BirthdayServiceImpl extends ServiceImpl<BirthdayMapper, Birthday> implements BirthdayService {

    private static final Logger log = LoggerFactory.getLogger(BirthdayServiceImpl.class);

    @Value("${tencent.birthday.one}")
    private Integer one;
    @Value("${tencent.birthday.two}")
    private Integer two;
    @Value("${tencent.birthday.three}")
    private Integer three;
    @Value("${tencent.birthday.four}")
    private Integer four;

    @Resource
    private MsgService msgService;
    @Resource
    private WeChatService weChatService;


    @Override
    public void wishes() {
        //获取今天日期
        List<Integer> list=new ArrayList<Integer>();
        list.add(one);
        list.add(two);
        list.add(three);
        list.add(four);

        Random r=new Random();
        int i=r.nextInt(4);
        Integer index=list.get(i);
        ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate(DateUtil.today()));
        int month=chineseDate.getMonth();
        int day=chineseDate.getDay();
        String time=month+"-"+day;
        //查询今天过生日的人
        LambdaQueryWrapper<Birthday> wrapper = Wrappers.lambdaQuery(Birthday.class);
        wrapper.eq(Birthday::getDay,time);
        List<Birthday> BirthdayList=baseMapper.selectList(wrapper);
        for (Birthday birthday : BirthdayList) {
            StringBuilder sb=new StringBuilder();
            String relationship=birthday.getRelationship();
            String phone = birthday.getPhone();
            sb.append(relationship).append("，我是杨文博");
            String[] params = new String[]{sb.toString()};
            if(StrUtil.isNotBlank(phone)){
                msgService.sendMsg("鹏鹏记录日志",phone, params,index);
                log.info(birthday.getName()+"-"+birthday.getYear()+"-"+time+"-生日");
            }
        }
    }

    @Override
    public void birth() {
        String accessToken=weChatService.getAccessToken();
        weChatService.sendMessage(accessToken,"33周加1天","2500g","9天");
    }


}
